package com.skyline.manage.feed.task;

import java.util.ArrayList;
import java.util.List;

import com.mongodb.DBCollection;

public class FeedManageTask extends java.util.TimerTask {

	private FeedDeleteTaskElement personalFeedDeleteTaskElement;
	private List<FeedMoveTaskElement> personalFeedMoveTaskElements = new ArrayList<FeedMoveTaskElement>();

	private FeedDeleteTaskElement subscribedFeedDeleteTaskElement;
	private List<FeedMoveTaskElement> subscribedFeedMoveTaskElements = new ArrayList<FeedMoveTaskElement>();

	public FeedManageTask(List<DBCollection> personalFeeds, String lifesStr, List<DBCollection> subscribedFeeds) {
		// 初始化lifes
		String[] lifesStrs = lifesStr.split(";");
		List<Integer> lifes = new ArrayList<Integer>();
		for (String life : lifesStrs) {
			lifes.add(Integer.valueOf(life)*24*3600*100);
		}
		
		this.initial(personalFeeds, lifes, subscribedFeeds);
	}

	public FeedManageTask(List<DBCollection> personalFeeds, List<Integer> lifes, List<DBCollection> subscribedFeeds) {
		this.initial(personalFeeds, lifes, subscribedFeeds);
	}
	
	public void initial(List<DBCollection> personalFeeds, List<Integer> lifes, List<DBCollection> subscribedFeeds){
		int i = 0;
		for (i = 0; i < personalFeeds.size() - 1; i++) {
			FeedMoveTaskElement e = new FeedMoveTaskElement(personalFeeds.get(i), personalFeeds.get(i + 1), lifes.get(i));
			personalFeedMoveTaskElements.add(e);
		}
		personalFeedDeleteTaskElement = new FeedDeleteTaskElement(personalFeeds.get(i), lifes.get(i));
		for (i = 0; i < subscribedFeeds.size() - 1; i++) {
			FeedMoveTaskElement e = new FeedMoveTaskElement(subscribedFeeds.get(i), subscribedFeeds.get(i + 1), lifes.get(i));
			subscribedFeedMoveTaskElements.add(e);
		}
		subscribedFeedDeleteTaskElement = new FeedDeleteTaskElement(subscribedFeeds.get(i), lifes.get(i));
	}

	@Override
	public void run() {
		personalFeedDeleteTaskElement.run();
		subscribedFeedDeleteTaskElement.run();
		int i = 0;
		for (; i < subscribedFeedMoveTaskElements.size(); i++) {
			personalFeedMoveTaskElements.get(i).run();
			subscribedFeedMoveTaskElements.get(i).run();
		}
		for (; i < personalFeedMoveTaskElements.size(); i++) {
			personalFeedMoveTaskElements.get(i).run();
		}
		// System.out.println(System.currentTimeMillis());
	}

}
